/**
 * Created by jocky on 2017/7/14.
 */
(function ($, sr) {
    var debounce = function (func, threshold, execAsap) {
        var timeout;

        return function debounced() {
            var obj = this, args = arguments;

            function delayed() {
                if (!execAsap)
                    func.apply(obj, args);
                timeout = null;
            }

            if (timeout)
                clearTimeout(timeout);
            else if (execAsap)
                func.apply(obj, args);

            timeout = setTimeout(delayed, threshold || 100);
        };
    };

    // smartresize
    jQuery.fn[sr] = function (fn) {
        return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr);
    };

})(jQuery, 'smartresize');

var CURRENT_URL = window.location.href.split('#')[1],
    $BODY = $('body'),
    $MENU_TOGGLE = $('#menu_toggle'),
    $SIDEBAR_MENU = $('#sidebar-menu');

/**
 * 菜单事件
 */
function init_menu() {
    //菜单栏目的显示和隐藏
    $SIDEBAR_MENU.find('.first_menu').on('click', function (e) {
        var $li = $(this).parent();

        if ($li.hasClass('active') || $li.hasClass('active-sm')) {
            $li.removeClass('active active-sm');
            $li.find('ul').slideUp();
        } else {
            //是否有打开的菜单项
            $.each($SIDEBAR_MENU.find('.first_menu').parent(), function (i, v) {
                if ($(v).hasClass('active') || $(v).hasClass('active-sm')) {
                    $(v).removeClass('active active-sm');
                    $(v).find('ul').slideUp();
                }
            });

            //开启此项
            $li.addClass('active');
            $('ul:first', $li).slideDown();
        }
    });

    //左侧菜单折叠按钮
    $MENU_TOGGLE.on('click', function () {
        if ($BODY.hasClass('nav-md')) {
            $SIDEBAR_MENU.find('li.active ul').hide();
            $SIDEBAR_MENU.find('li.active').addClass('active-sm').removeClass('active');
        } else {
            $SIDEBAR_MENU.find('li.active-sm ul').show();
            $SIDEBAR_MENU.find('li.active-sm').addClass('active').removeClass('active-sm');
        }

        $BODY.toggleClass('nav-md nav-sm');
    });

    //菜单项
    $SIDEBAR_MENU.find('a[data-href!=none]').on('click', function (e) {
        if ($(this).attr('data-href') == 'onclick') return false;

        var isFirstMenu = $(this).hasClass('first_menu');

        CURRENT_URL = $(this).attr('data-href');
        window.location.hash = CURRENT_URL;

        var menu_id;
        menu_id = $(this).parent().attr('id');

        //去除样式
        $.each($SIDEBAR_MENU.find('a'), function (i, v) {
            var tmpObj2;
            if ($(v).hasClass('first_menu')) {
                tmpObj2 = $(v);
            } else {
                tmpObj2 = $(v).parent()
            }

            if (tmpObj2.hasClass('current-page')) {
                tmpObj2.removeClass('current-page');
                return false;
            }
        });

        //添加样式
        var tmpObj;
        if (isFirstMenu) {
            tmpObj = $(this);
        } else {
            tmpObj = $(this).parent();
        }
        tmpObj.addClass('current-page');


        //内容框的显示和隐藏
        $.each($(".content_detail"), function (i, v) {
            if ($(v).attr('id') == menu_id + '_div') {
                $(v).show();
            } else {
                $(v).hide();
            }
        });

        //获取内容
        get_content(menu_id);
    });


    //左侧菜单导航固定长度
    $('#menu_fixed').mCustomScrollbar({
        autoHideScrollbar: true,
        theme: 'minimal',
        mouseWheel: {preventDefault: true}
    });
}